/*
 * @Author: 吴世扬 18368095041@163.com
 * @Date: 2025-02-21 16:41:05
 * @LastEditors: 吴世扬 18368095041@163.com
 * @LastEditTime: 2025-02-27 16:09:13
 * @FilePath: /vue3-admin-server/src/admin/v1/support/file.controller.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import {
    Controller,
    Get,
    Post,
    UseGuards,
    Param,
    UseInterceptors,
    UploadedFile,
    Body,
    Req,
    Res
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { AuthGuard } from 'src/guards/auth.guard';
import { FileService } from 'src/shareModules/file/file.module';
import { ResponseService } from 'src/shareModules/common/common.module';
import { generateObjFromError } from 'src/common/utils';
import { UploadFileDTO } from 'src/common/dto';
import { FileTypeEnum } from 'src/common/constants';

@Controller('admin/api/v1/file')
@UseGuards(AuthGuard)
export class FileController {
    constructor(
        private readonly fileService: FileService,
        private readonly responseService: ResponseService
    ) {}

    @Get('/:token')
    async readFile(@Param('token') token: string, @Res() res: HwResponse) {
        try {
            const { buffer, type } = await this.fileService.readFile(token);
            // 根据文件类型设置正确的 Content-Type
            switch (type) {
                case FileTypeEnum.IMAGE:
                    res.setHeader('Content-Type', 'image/png'); // 根据实际情况调整文件类型
                    break;
                case FileTypeEnum.VIDEO:
                    res.setHeader('Content-Type', 'video/mp4'); // 根据实际情况调整文件类型
                    break;
                case FileTypeEnum.AUDIO:
                    res.setHeader('Content-Type', 'audio/mpeg'); // 根据实际情况调整文件类型
                    break;
                case FileTypeEnum.WORD:
                    res.setHeader('Content-Type', 'application/msword'); // 根据实际情况调整文件类型
                    break;
                case FileTypeEnum.PDF:
                    res.setHeader('Content-Type', 'application/pdf'); // 根据实际情况调整文件类型
                    break;
                default:
                    res.setHeader('Content-Type', 'application/octet-stream'); // 默认类型
                    break;
            }
            res.send(buffer);
        } catch (e) {
            return this.responseService.error(
                undefined,
                generateObjFromError(e as Error)
            );
        }
    }

    // @Post('/upload')
    // async uploadFile(@Body() body: any) {
    //     return this.fileService.uploadFile(body);
    // }

    @Post('upload')
    @UseInterceptors(FileInterceptor('file'))
    async uploadFile(
        @UploadedFile() file: Express.Multer.File,
        @Body() uploadFileDTO: UploadFileDTO,
        @Req() req: HwRequest
    ): Promise<
        VO.ResultVO<{ id: number; token: string } | Record<string, unknown>>
    > {
        try {
            console.log(file);
            const { id, token } = await this.fileService.uploadFile(
                file,
                uploadFileDTO,
                req.tokenPayload?.id
            );
            return this.responseService.success(undefined, {
                id,
                token
            });
        } catch (e) {
            return this.responseService.error(
                undefined,
                generateObjFromError(e as Error)
            );
        }
    }

    // @Post('/upload/base64')
}
